package io.vertx.kue

import io.vertx.kue.queue.*
import io.vertx.kue.queue.JobState
import io.vertx.kue.service.JobService
import io.vertx.core.AsyncResult
import io.vertx.core.Handler
import io.vertx.core.Vertx
import io.vertx.core.eventbus.Message
import io.vertx.core.json.JsonArray
import io.vertx.core.json.JsonObject


/**
 * Implementation of [io.vertx.blueprint.kue.CallbackKue].
 *
 * @author Eric Zhao
 */
class CallbackKueImpl(vertx: Vertx, config: JsonObject) : CallbackKue {

    private val kue: Kue
    private val jobService: JobService

    init {
        this.kue = Kue(vertx, config)
        this.jobService = kue.jobService
    }

    override fun createJob(type: String, data: JsonObject): Job {
        return kue.createJob(type, data)
    }

    override fun saveJob(job: Job, handler: Handler<AsyncResult<Job>>): CallbackKue {
        job.save().setHandler(handler)
        return this
    }

    override fun jobProgress(job: Job, complete: Int, total: Int, handler: Handler<AsyncResult<Job>>): CallbackKue {
        job.progress(complete, total).setHandler(handler)
        return this
    }

    override fun jobDoneFail(job: Job, ex: Throwable): CallbackKue {
        job.done(ex)
        return this
    }

    override fun jobDone(job: Job): CallbackKue {
        job.done()
        return this
    }

    override fun <R> on(eventType: String, handler: Handler<Message<R>>): CallbackKue {
        kue.on(eventType, handler)
        return this
    }

    override fun process(type: String, n: Int, handler: Handler<Job>): CallbackKue {
        kue.process(type, n, handler)
        return this
    }

    override fun processBlocking(type: String, n: Int, handler: Handler<Job>): CallbackKue {
        kue.processBlocking(type, n, handler)
        return this
    }

    override fun getJob(id: Long, handler: Handler<AsyncResult<Job>>): CallbackKue {
        jobService.getJob(id, handler)
        return this
    }

    override fun removeJob(id: Long, handler: Handler<AsyncResult<Void>>): CallbackKue {
        jobService.removeJob(id, handler)
        return this
    }

    override fun existsJob(id: Long, handler: Handler<AsyncResult<Boolean>>): CallbackKue {
        jobService.existsJob(id, handler)
        return this
    }

    override fun getJobLog(id: Long, handler: Handler<AsyncResult<JsonArray>>): CallbackKue {
        jobService.getJobLog(id, handler)
        return this
    }

    override fun jobRangeByState(state: String, from: Long, to: Long, order: String, handler: Handler<AsyncResult<List<Job>>>): CallbackKue {
        jobService.jobRangeByState(state, from, to, order, handler)
        return this
    }

    override fun jobRangeByType(type: String, state: String, from: Long, to: Long, order: String, handler: Handler<AsyncResult<List<Job>>>): JobService {
        jobService.jobRangeByType(type, state, from, to, order, handler)
        return this
    }

    override fun jobRange(from: Long, to: Long, order: String, handler: Handler<AsyncResult<List<Job>>>): JobService {
        jobService.jobRange(from, to, order, handler)
        return this
    }

    override fun cardByType(type: String, state: JobState, handler: Handler<AsyncResult<Long>>): CallbackKue {
        jobService.cardByType(type, state, handler)
        return this
    }

    override fun card(state: JobState, handler: Handler<AsyncResult<Long>>): CallbackKue {
        jobService.card(state, handler)
        return this
    }

    override fun completeCount(type: String, handler: Handler<AsyncResult<Long>>): CallbackKue {
        jobService.completeCount(type, handler)
        return this
    }

    override fun failedCount(type: String, handler: Handler<AsyncResult<Long>>): CallbackKue {
        jobService.failedCount(type, handler)
        return this
    }

    override fun inactiveCount(type: String, handler: Handler<AsyncResult<Long>>): CallbackKue {
        jobService.inactiveCount(type, handler)
        return this
    }

    override fun activeCount(type: String, handler: Handler<AsyncResult<Long>>): CallbackKue {
        jobService.activeCount(type, handler)
        return this
    }

    override fun delayedCount(type: String, handler: Handler<AsyncResult<Long>>): CallbackKue {
        jobService.delayedCount(type, handler)
        return this
    }

    override fun getAllTypes(handler: Handler<AsyncResult<List<String>>>): CallbackKue {
        jobService.getAllTypes(handler)
        return this
    }

    override fun getIdsByState(state: JobState, handler: Handler<AsyncResult<List<Long>>>): CallbackKue {
        jobService.getIdsByState(state, handler)
        return this
    }

    override fun getWorkTime(handler: Handler<AsyncResult<Long>>): CallbackKue {
        jobService.getWorkTime(handler)
        return this
    }
}
